$(function() {
	showCondition(true, 0, 0, 1, []);
	
	var str='<table id="mdc_monitor" class="table display nowrap" cellspacing="0" width="100%" role="grid" aria-describedby="example_info" style="width: 100%;">';
	str +='<caption class="contaotalsccq" style="display:none">各MDC专业指标分析</caption>';
	str +='</table>';
	$('#'+tabId+ ' .tab-content').html(str);
	//加载指标配置代码和描述
	getQuotas("mdcMonitor", loadData, "mdc_monitor");
	function loadData(totalQuota) {
		loadMonitorQuotas(totalQuota, "mdc_monitor");
	}
	function loadMonitorQuotas(totalQuota, divId) {
		// 获取监测指标
		var newQuotas = [];
		getQuotas(totalQuota);
		function getQuotas(quotas) {
			for ( var o in quotas) {
				var quota = quotas[o];
				if (quota.children) {
					getQuotas(quota.children);
				} else {
					newQuotas.push(quota);
				}
			}
		}
		var datas = [];
		var param=drgUI.setAjaxData(tabId);
		param.monitorType='mdc';
		param.isGetCc='0';
		param.quotaCode='Base';
		// 获取基础指标
		$.post(contextPath + "/DrgsIndexCal/indexCal.dhc", param).done(function(data) {
			if (data.data != undefined) {
				datas = datas.concat(data.data);
			}
			$('#' + tabId + ' #mdc_monitor').remove();
			$('#'+tabId+ ' .tab-content').html(str);
			concatTableData(newQuotas, datas, divId);
		})
	}
})

/**
 *  @ Discription:拼接指标表格
 * @Autor ：liuhongjun
 * @param newQuotas
 * @param datas
 * @param divId
 * @returns any mdcMonitor.js 2017年6月11日下午5:03:35
 */
function concatTableData(newQuotas, datas, divId){	
	 var  columns = [ {
		"title":"MDC",
		"data" : "ccode1",
		"width" : 320,
		"render" : function(obj, type, full) {
			if (full.ccode != null) {
				return  full.ccode + "-" + full.ddesc;
			} else {
				return "未入组";
			}
		}
	}];	
	 // 添加 理论DRG组数 指标 
	 newQuotas.splice(3, 0, {"islink":"0","code":"DrgCountInMdc","name":"理论DRG组数"}) ;
	 newQuotas.splice(4, 0, {"islink":"0","code":"DrgCountNotInMdc","name":"未覆盖DRG病组数"}) ;
	var ajaxCount = newQuotas.length;	
	var i=0;	
	configQuotaFlag(newQuotas[i],i,newQuotas);
	//对非基础指标列进行拼接，用递归方法解决循环与多个ajax请求异步问题
	function configQuotaFlag(data,cnt,array){
		  var value=data.code;
		  var title=data.name;
		  var isLink=data.islink
			//对有病例明细的列添加链接
			if(isLink=="1"){
				columns.push({
					"title":title,
					"data": value,
					"type":"number-html",
					"render": function(obj, type, full){
						if(full[value]!=undefined && full[value]!=0){
						return "<a href=\"javascript:loadCaseDtl('"+full.ccode+full.ddesc+"','"+full.ccode+"','"+'mdc'+"', '"+value+"')\">"+formatNumber(full[value])+"</a>";
							}else{
								return 0;
							}
						}
				});
			}else if(value == "DrgCountNotInMdc"){
				columns.push({
					"title":title,
					"data": value,
					"type":"number-html",
					"render": function(obj, type, full){
						if(full[value]!=undefined && full[value]!=0){
							return "<a href=\"javascript:showNotCoverDRGsDetail('"+full.ccode+"')\">"+formatNumber(full[value])+"</a>";
						}else{
							return 0;
						}
					}
				});
			}else{
				columns.push({ 
					"title":title,
					"data": value,
					"render": function(obj, type, full){
						if(full[value]!=undefined){
							return formatNumber(full[value]);
							}else{
								return 0;
							}
						}
					});
			}
		  
		  if(value == "DrgCountInMdc"){
			  columns.push({ 
					"title":"病组覆盖率(%)",
					"data": "DrgsCoverageRation",
					"render": function(obj, type, full){
						if(full["DRGCount"] && full["DrgCountInMdc"]) {
							return (full["DRGCount"]/full["DrgCountInMdc"]*100).toFixed(2) ;
						} else {
							return 0;
						}
					}
			  });
		  }
		  
			/**
			 * 根据指标获取配置获取数据
			 */
			getTableData(value, 'mdc', function(data, quotaCode){
				var data_s = [];
				data_s = data_s.concat(data);
				//合并list
				mergeListcc(datas, data_s,'ccode','',quotaCode);
				ajaxCount--;
			})
			if (cnt + 1 < array.length) {
				configQuotaFlag(array[cnt + 1], cnt + 1, array);
			} else
				return false;
	}
		
	var timer = setInterval(function(){
		if(ajaxCount==0){
			clearInterval(timer);
			drawTable(datas, columns, divId);
		}
	}, 500);
}

/**
 * @ Discription:绘制表格
 * @Autor ：liuhongjun
 * @param data 拼接好的数据
 * @param columns 定义datatables每列的列属性
 * @param divId 表格位置
 * @returns
 * any
 * mdcMonitor.js
 * 2017年6月11日下午5:04:19
 */
function drawTable(data, columns, divId){
	var paramsDataTables={info:false,scrollX:true,rownumbers:true,order:[[ 4, "desc" ]],tableTitle:'各MDC专业指标分析表',fixedColumns:{'leftColumns' :1}};
	var drawCallback=function( oSettings ) {
	    //获取datatables标题,供导出时获取
       	oSettings.expertName = this.find(".contaotalsccq").text();
	    	$(this).rowspan(0);
	    }
	paramsDataTables.drawCallback=drawCallback ;
	$("#"+tabId+" #"+divId).dataTableBasic(columns,data,paramsDataTables);
}

/**
 * 
 * @ Discription:根据指标发送请求获取数据
 * @Autor ：liuhongjun
 * @param quotaCode
 * @param monitorType
 * @param callback
 * @returns
 * any
 * mdcMonitor.js
 * 2017年6月11日下午5:05:50
 */
function getTableData(quotaCode, monitorType, callback){
   var param=drgUI.setAjaxData(tabId);
	param.monitorType=monitorType;
	param.isGetCc='0';
	param.quotaCode=quotaCode;
	$.post(contextPath + "/DrgsIndexCal/indexCal.dhc",param).done(function(res){
			var data =[]
			if (res.data != undefined){
				data=res.data;
			}
			callback(data, quotaCode);
		})
}
//加载病组详情表格
function showNotCoverDRGsDetail(mdcCode) {
	$.post(contextPath + '/pages/common/top-dialog.jsp').done(function(data){
		var dialog  = $(data);
		dialog.find(".modal-title").html("未覆盖DRG病组详情");
		dialog.find(".modal-footer").css("border-top","");
		dialog.modal({
			keyboard: false
		}).on({
			'hidden.bs.modal' : function(){
				$(this).remove();
			},
			'shown.bs.modal' : function(){
				dialog.find("#echartcq4").html("<table id = 'drgDetail' class='display' width = '100%'><tbody></tbody></table>");
				var param = drgUI.setAjaxData(tabId);
				param.parmdcCode = mdcCode;
				$.post(contextPath + "/MDCMonitor/showNotCoverDRGsDetail.dhc", param).done(function(result) {
					var data = result.data ;
					var columns=[
				                  	{"data": "DRG_CODE","title":'DRG',
				                  		"render": function(obj, type, full) {
				                  			return full["DRG_CODE"]+(full["DRG_DESC"]==undefined?"":("-"+full["DRG_DESC"]));
					                     }
				                  	},
				                  	{"data": "AS_WEIGHT","title":'权重',
				                  		"render": function(obj, type, full) {
				                  			return formatNumber(full["AS_WEIGHT"],2) ;
					                     }
				                  	},
				                  	{"data": "AS_AVG_FEE","title":'次均费用(元)',
				                  		"render": function(obj, type, full) {
				                  			return formatNumber(full["AS_AVG_FEE"],2) ;
					                     }
				                  	},
				                  	{"data": "AS_AVG_DAYS","title":'平均住院日',
				                  		"render": function(obj, type, full) {
				                  			return formatNumber(full["AS_AVG_DAYS"],2) ;
					                     }
				                  	},
				                  	{"data": "AS_RISK_LEVEL","title":'风险等级',
				                  		"render": function(obj, type, full) {
				                  			var risklevel=getDicDesc("RiskLevel",full["AS_RISK_LEVEL"])=='未定义'?"":getDicDesc("RiskLevel",full["AS_RISK_LEVEL"]);
				            				return risklevel ;
					                     }
				                  	}
				                 ];
					var paramsDataTables={searching:true,lengthChange:true,ordering:true,info:false,scrollY:'280px',tableTitle:"未覆盖DRG病组明细"};
					dialog.find("#drgDetail").dataTableBasic(columns,data,paramsDataTables);
				});
			}
		})
	})
	
}
